package edu.hnu.cloudflight.android;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.baidu.push.example.LoginActivity;

import edu.hnu.cloudflight.android.domain.City;
import edu.hnu.cloudflight.android.domain.CityRequest;
import edu.hnu.cloudflight.android.domain.Flight;
import edu.hnu.cloudflight.android.util.FlightCitiesUtil;
import edu.hnu.cloudflight.android.util.HttpUtil;
import edu.hnu.cloudflight.android.util.StringUtil;

public class MyApplication extends Application {

	private static final String TAG = MyApplication.class.getSimpleName();
	
	public static String appToken;

	public static Resources res;

	public static String queryByFlightNumUrl;
	public static String queryAllCitiesUrl;
	public static String queryByFlightCourseUrl;
	public static String submitReserveUrl;
	public static String key = "6c252b3942da3a050ee91bb7a3544caa";
	public static List<Flight> queryByFlightCourseResult;
	
	public static int initTimes;
	private boolean isInited = false;

	@Override
	public void onCreate() {

		new Thread(initThread) {
		}.start();

	}
	
	
	
	public static void baiduPushSetTags(Context con){
		String token = LoginActivity.getAccessToken(con);

		if(token!=null&&!"".equals(token)){
			Log.i(TAG, "App token is "+token);
			appToken=token;
			/*String[] tags = token.split("/.");
			List<String> tagArray = new ArrayList<String>();
			for(String tag:tags){
				tagArray.add(tag);
			}*/
			/*List<String> tagArray = new ArrayList<String>();
			tagArray.add("abc");
			tagArray.add("def");
			tagArray.add("123");
			PushManager.setTags(con, tagArray);*/
			Log.i(TAG, "BaiDuPush 标签已经设置！");
		}
	}

	Runnable initThread = new Runnable() {

		@Override
		public synchronized void run() {
			if (isInited == true) {
				return;
			}
			/*PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY, Utils.getMetaValue(MyApplication.this, "api_key"));
			Log.i(TAG, "BaiDuPush 已经初始化！");
			
			baiduPushSetTags(getApplicationContext());*/
			
			res = MyApplication.this.getResources();
			queryByFlightNumUrl = res.getString(R.string.url_querybyflightnum);
			queryAllCitiesUrl = res.getString(R.string.url_getallcities);
			queryByFlightCourseUrl = res.getString(R.string.url_querybyflightcourse);
			submitReserveUrl = res.getString(R.string.url_submitreserve);
			try {
				Map<String,String>  params = new HashMap<String, String>();
				params.put("key", MyApplication.key);
				String result = HttpUtil.getStringByGet(queryAllCitiesUrl,params);
				CityRequest cityRequest = JSON.parseObject(result, CityRequest.class);
				Log.i(TAG, cityRequest.resultcode);
				if ("200".equals(cityRequest.resultcode)) {
					for (City c : cityRequest.result) {
						if ("重庆".equals(c.city)) {
							c.pinyin = "cq";
						} else if ("长沙".equals(c.city)) {
							c.pinyin = "cs";
						} else {
							String pinyin = StringUtil.converterToFirstSpell(c.city);
							c.pinyin = pinyin;
						}
					}
					FlightCitiesUtil.flightcities = cityRequest.result;
					Log.i(TAG, "航班城市已初始化完成！");
				}
				isInited = true;
				Log.i(TAG, "App以初始化！");
			} catch (NotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	};
}
